package com.hbx.service.impl;

import com.hbx.constant.TableEnum;
import com.hbx.logs.OperaTypeEnum;
import com.hbx.logs.annotation.MarkLog;
import com.hbx.model.ResourceType;
import com.hbx.mapper.ResourceTypeMapper;
import com.hbx.service.ResourceTypeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hbx.utils.Result;
import com.hbx.vo.res.insertOrUpdateResTypeDto;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hbx
 * @since 2022-02-12
 */
@Service
public class ResourceTypeServiceImpl extends ServiceImpl<ResourceTypeMapper, ResourceType> implements ResourceTypeService {

    @Autowired
    private ResourceTypeMapper resourceTypeMapper;

    @Override
    @MarkLog(operate = OperaTypeEnum.UPDATE, tables = TableEnum.RESOURCE_TYPE, desc = "管理员添加或修改分类")
    public void insertOrUpdateResType(insertOrUpdateResTypeDto dto, Result r) {
        Integer resourceTypeId = dto.getResourceTypeId();
        if (resourceTypeId != null){
            // 修改
            ResourceType type = new ResourceType();
            BeanUtils.copyProperties(dto, type);
            int result = resourceTypeMapper.updateById(type);
            if (result != 1){
                r.setCode(201).setMsg("修改失败");
                return;
            }
            r.setCode(200).setMsg("修改成功");
        } else {
            ResourceType type = new ResourceType();
            BeanUtils.copyProperties(dto, type);
            int result = resourceTypeMapper.insert(type);
            if (result != 1){
                r.setCode(201).setMsg("添加失败");
                return;
            }
            r.setCode(200).setMsg("添加成功");
        }
    }

    @Override
    @MarkLog(operate = OperaTypeEnum.DELETE, tables = TableEnum.RESOURCE_TYPE, desc = "管理员删除分类")
    public void removeType(Integer resTypeId, Result r) {
        int result = resourceTypeMapper.deleteById(resTypeId);
        if (result != 1){
            r.setCode(201).setMsg("删除失败");
            return;
        }
        r.setCode(200).setMsg("删除成功");
    }
}
